﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace BovespaUsageRules
{
    public sealed class Rule_AvoidUseGoToStatement : BaseAddIn.Rules.BaseRuleAddIn
    {
        public Rule_AvoidUseGoToStatement()
            : base("BovespaAvoidUseGoToStatement",
                   "BovespaUsageRules.BovespaUsageRules",
                   typeof(Rule_AvoidUseGoToStatement).Assembly)
        {
        }

        protected sealed override void Check(CodeFunction funcao, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            AnalisaGoto((CodeElement)funcao, namespaceElement, classe);
        }

        protected sealed override void Check(CodeProperty propriedade, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            AnalisaGoto((CodeElement)propriedade, namespaceElement, classe);
        }

        private void AnalisaGoto(CodeElement elemento, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            EditPoint edit = elemento.StartPoint.CreateEditPoint();
            string texto = Common.Rotinas.GetTextNoComent(edit.GetText(elemento.EndPoint));

            if (texto.Contains("goto ")
                || texto.Contains("goto\r\n"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                 elemento.StartPoint.Line,
                                 elemento.StartPoint.DisplayColumn,
                                 "AvoidUseGoToStatementResolution",
                                 classe.Name,
                                 elemento.Name,
                                 namespaceElement.Name);
            }
        }
    }
}
